var that;
//级联对象
function  cascadeObject(){
	this.cascadeCooperorg();
	that = this;
}

cascadeObject.prototype={
	
	//合作机构
	cascadeCooperorg:function(){
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeCooperorg';
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			data:'',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				$("input[name=cooperorgCode]").after("<select style='width:173px;height:25px;' id='cooperorgCode' name='cooperorgCode'></select>");
				$("input[name=cooperorgCode]").remove();
				$("input[name=contractCode]").after("<select style='width:173px;height:25px;' id='contractCode' name='contractCode'></select>");
				$("input[name=contractCode]").remove();
				$("input[name=agreementCode]").after("<select style='width:173px;height:25px;' id='agreementCode' name='agreementCode'></select>");
				$("input[name=agreementCode]").remove();
				$("select[name=cooperorgCode]").change(function(){
					var value = $("select[name=cooperorgCode]").val();
					that.cascadeContract(value);
					that.cascadeAgreement(value);
				});
				var result=results.data;
				var j;
				var resultLength = result.length - 1;
				$("select[name=cooperorgCode]").html("<option value=''>--请选择--</option>");
				$("select[name=contractCode]").html("<option value=''>--请选择--</option>");
				$("select[name=agreementCode]").html("<option value=''>--请选择--</option>");
				for (j = 0; j <= resultLength; j++) {
					$("select[name=cooperorgCode]").append("<option value='"+result[j].cooperorgCode+"'>"+result[j].cooperorgName+"</option>");
					/*if(null != cooperorgCode && "" !=cooperorgCode){
						
						if(cooperorgCode == result[j].cooperorgCode){
							$("select[name=cooperorgCode]").find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
							cascadeContract($('#cooperorg'),contractCode);
						}
					}*/
				}
				
				
			}
			
			
		});
	},
	
	//协议
	cascadeAgreement:function(value){
		$("select[name=agreementCode] option").remove();
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeAgreement?cooperorgCode=' + value;
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				var result=results.data;
				$("select[name=agreementCode]").html("<option value=''>--请选择--</option>");
				var j;
				var resultLength = result.length - 1;
				for (j = 0; j <= resultLength; j++) {
					$("select[name=agreementCode]").append("<option value='"+result[j].agreementCode+"'>"+result[j].agreementName+"</option>");
					/*if(null != contractCode && "" !=contractCode){
						
						if(contractCode == result[j].contractCode){
							$('#cooperorg').find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
						}
					}*/
				}
			}
			
		});
	},

	
	//合约
	cascadeContract:function(value){
		$("select[name=contractCode] option").remove();
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeContract?cooperorgCode=' + value;
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				var result=results.data;
				$("select[name=contractCode]").html("<option value=''>--请选择--</option>");
				var j;
				var resultLength = result.length - 1;
				for (j = 0; j <= resultLength; j++) {
					$("select[name=contractCode]").append("<option value='"+result[j].contractCode+"'>"+result[j].contractName+"</option>");
					/*if(null != contractCode && "" !=contractCode){
						
						if(contractCode == result[j].contractCode){
							$('#cooperorg').find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
						}
					}*/
				}
			}
			
		});
	}
};

(function( $ ){  
	  $.fn.newCascadeObject = function() {
	   	return new cascadeObject();
	  };  
})(jQuery);

$(document).ready(function(){
	$("select[name=cooperorgCode]").newCascadeObject();
});